package top.qitun.controller;

import java.util.Date;
import java.util.Optional;

import org.apache.commons.lang.StringUtils;
import org.assertj.core.util.Preconditions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import top.qitun.bizz.shop.ttable.entity.TTable;
import top.qitun.bizz.shop.ttable.entity.TTableBO;
import top.qitun.bizz.shop.ttable.service.TTableService;
import top.qitun.modal.request.TableCreateRequest;
import top.qitun.modal.request.TableUpdateRequest;
import top.qitun.model.response.ObjectCollectionResponse;
import top.qitun.model.response.ObjectDataResponse;
import top.qitun.util.OrikaMapper;

/**
 * 台号
 * 
 * @author Cloudwei
 *
 */
@RestController
@RequestMapping(value = "/v1")
public class TableController {
	Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private TTableService tableService;

	@ApiOperation(value = "创建台号", notes = "用户创建新台号")
	@RequestMapping(value = "/table", method = RequestMethod.POST)
	public ObjectDataResponse<TTable> create(@RequestBody TableCreateRequest request) {
		String desc = "创建台号";
		logger.debug(desc + " 开始");
		try {
			TTable table = null;
			Preconditions.checkNotNull(request);
			Date now = new Date();
			table = OrikaMapper.map(request, TTable.class);
			table.setCreateTime(now);
			table.setLastUpdateTime(now);
			table.setStatus(StatusCode.table_empty.code());
			tableService.add(table);
			return new ObjectDataResponse<TTable>(0, "创建店铺成功", table);
		} catch (Exception e) {
			logger.error(desc + "失败", e);
			return new ObjectDataResponse<>(-1, desc + "失败.请联系客服" + e.getMessage());
		}
	}
	
	@ApiOperation(value = "修改坐台信息", notes = "用户修改台号")
	@RequestMapping(value = "/table/{id}", method = RequestMethod.POST)
	public ObjectDataResponse<TTable> update(@PathVariable(value = "id") Integer id,@RequestBody TableUpdateRequest request) {
		String desc = "创建台号";
		logger.debug(desc + " 开始");
		try {
			
			Optional<TTable> optional = Optional.ofNullable(tableService.queryById(id));
			return optional.map(table->{
				Date now = new Date();
				table.setLastUpdateTime(now);
				if(StringUtils.isNotEmpty(request.getName()))
					table.setName(request.getName());
				if(request.getNo()!=null)
					table.setNo(request.getNo());
				if(request.getAmount()!=null)
				table.setAmount(request.getAmount());
				try {
					tableService.update(table);
				} catch (Exception e) {
					logger.error(desc + "失败", e);
					return new ObjectDataResponse<TTable>(-1, desc + "失败.请联系客服" + e.getMessage());
				}
				return new ObjectDataResponse<TTable>(0, "修改店铺成功", table);
			}).orElse(new ObjectDataResponse<>(1, "台号不存在"));
			
			
		} catch (Exception e) {
			logger.error(desc + "失败", e);
			return new ObjectDataResponse<>(-1, desc + "失败.请联系客服" + e.getMessage());
		}
	}
	
	@ApiOperation(value = "修改坐台状态", notes = "用户修改台号")
	@RequestMapping(value = "/table/{id}/status", method = RequestMethod.POST,name="updateStatus")
	public ObjectDataResponse<TTable> updateStatus(@PathVariable(value = "id") Integer id,@RequestBody TableUpdateRequest request) {
		String desc = "创建台号";
		logger.debug(desc + " 开始");
		try {
			
			Optional<TTable> optional = Optional.ofNullable(tableService.queryById(id));
			return optional.map(table->{
				Date now = new Date();
				table.setStatus(request.getStatus());
				table.setLastUpdateTime(now);
				try {
					tableService.update(table);
				} catch (Exception e) {
					logger.error(desc + "失败", e);
					return new ObjectDataResponse<TTable>(-1, desc + "失败.请联系客服" + e.getMessage());
				}
				return new ObjectDataResponse<TTable>(0, "修改店铺成功", table);
			}).orElse(new ObjectDataResponse<>(1, "台号不存在"));
			
			
		} catch (Exception e) {
			logger.error(desc + "失败", e);
			return new ObjectDataResponse<>(-1, desc + "失败.请联系客服" + e.getMessage());
		}
	}

	@ApiOperation(value = "显示店铺内的餐桌", notes = "显示店铺内的餐桌")
	@RequestMapping(value = "/shop/{shopid}/tables", method = RequestMethod.GET)
	public ObjectCollectionResponse<TTable> tableList(@PathVariable(value = "shopid") Integer shopid) {
		String desc = "显示店铺内的餐桌";
		logger.debug(desc + " 开始");
		try {
			return Optional.ofNullable(tableService.queryShopTables(shopid))
					.map(TableList -> new ObjectCollectionResponse<TTable>(0, desc + "成功", TableList))
					.orElse(new ObjectCollectionResponse<>(1, "无数据"));
		} catch (Exception e) {
			logger.error(desc + "失败", e);
			return new ObjectCollectionResponse<>(-1, desc + "失败.请联系客服" + e.getMessage());
		}
	}
	
	@ApiOperation(value = "根据id查询桌面信息详情", notes = "查询餐桌详情")
	@RequestMapping(value = "/table/{id}", method = RequestMethod.GET, name = "queryTable")
	public ObjectDataResponse<TTableBO> queryTable(@ApiParam(example="1",value="台面Id") @PathVariable(value = "id") Integer id) {
		String desc = "查询桌面详情";
		try {
			Optional<TTableBO> optionaul = Optional.ofNullable(tableService.queryById(id));
			return optionaul
					.map(table -> new ObjectDataResponse<TTableBO>(0, desc + "成功", table))
					.orElse(new ObjectDataResponse<>(1, "无数据"));
		} catch (Exception e) {
			logger.error(desc + "失败", e);
			return new ObjectDataResponse<>(-1, desc + "失败.请联系客服" + e.getMessage());
		}
	}
}
